<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <el-card shadow="never" :body-style="{ padding: '10px 10px 0' }" class="mt10">
      <div slot="header" class="f16">
        <span style="font-weight: 600" class="mr55">产量明细查询</span>
      </div>
      <div class="flex mt10" style="align-items: center">
        <div class="f14 mr55">PS:扫描指令单标签时，请确认光标置于此栏位</div>
        <div class="f14 mr55">
          <span class="mr10">扫描条码</span>
          <el-input @keydown="(event:any) => handleKeyDown(event)" v-model="scanCode" style="width: 414px; height: 25px; font-size: 20px" />
        </div>
        <div class="flex AIC">
          <span class="mr10">扫描信息</span>
          <div :class="flagBC === '0' ? '' : flagBC === '1' ? 'susses' : 'error'" style="width: 250px; height: 25px; border: 1px solid black">
            {{ scanMessage }}
          </div>
        </div>
      </div>
    </el-card>

    <el-card shadow="never" :body-style="{ padding: '10px' }">
      <div slot="header" class="f16">
        <span style="font-weight: 600">成型完成采集扫描记录</span>
      </div>
      <Table
        selection
        :tableHeader="tableHeader"
        :tableData="tableData"
        class="mt10"
        :outTableHeight="160"
        :isLoading="isLoading"
        :summaryData="summaryData"
        summary
        @selectList="selectTableList"
      />
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { AcquisitionModel, selectList } from '@/plugins/mixin'
import LoginWork from '@/utils/LoginWork'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'InboundScanningOperation'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const scanCode = ref('')
const flagBC = ref('0')
const scanMessage = ref('')

const handleKeyDown = async (e: any) => {
  if (e.key === 'Enter' || e.keyCode === 13) {
    const res = (await LoginWork.post(`/pack/scanCollectAll?choose=${15}`, {
      lotNo: scanCode.value.toUpperCase()
      // prodType: radio.value
    })) as any
    if (res.code === 200) {
      JMessage.success('扫描成功')
      getTableList()
      const tempCode = JSON.parse(JSON.stringify(scanCode.value))
      scanCode.value = ''
      flagBC.value = '1'
      scanMessage.value = tempCode
    } else {
      JMessage.error(res.message)
      flagBC.value = '2'
      scanMessage.value = res.message
    }
  }
}
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入货号',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectP: '品牌',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectLabel: '',
    selectP: '采集员',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '完成日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '删除扫描记录'
  },
  {
    type: 'button',
    checkboxLabel: '查看所有采集资料'
  }
])

const tableHeader = ref([
  {
    label: '指令号',
    prop: 'lotNo',
    width: '160px'
  },
  {
    label: '品牌',
    prop: 'brand'
  },
  {
    label: '货号',
    width: '256px',
    prop: 'style'
  },
  {
    label: '颜色',
    width: '256px',
    prop: 'color'
  },
  {
    label: '号码',
    width: '110px',
    prop: 'sizeNo'
  },
  {
    label: '订单数量',
    width: '110px',
    prop: 'orderQty'
  },
  {
    label: '入库数量',
    width: '110px',
    prop: 'inQty'
  },
  {
    label: '客户交期',
    prop: 'deliveryDate'
  },
  {
    label: '采集员',
    width: '140px',
    prop: 'prodUserName'
  },
  {
    label: '扫描时间',
    width: '245px',
    prop: 'prodTime'
  }
])
const tableData = ref([])

// 表一模块
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '删除扫描记录') {
    if (selectedLists.value.length === 0) {
      JMessage.warning('请选择要删除的记录')
      return
    }
    const res = (await LoginWork.post('/pcOrder/out/pcOrderDataDeleteRecord', {
      list: selectedLists.value.map((item: any) => item.lotNo),
      choose: 1
    })) as any
    if (res.code === 200) {
      JMessage.success('删除成功')
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '查看所有采集资料') {
    getTableList(2)
  }
}
const imgList = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 6,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async (flag: any = 1) => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(`/pcOrder/getPcOrderDataRsj?choose=${flag}`, {
    flag,
    brand: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    completeEndTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '完成日期'
    )?.dateValue?.[1],
    completeStartTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '完成日期'
    )?.dateValue?.[0],
    deliveryEndTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[1],
    deliveryStartTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[0],
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令号'
    )?.inputValue,
    prodUserName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '采集员')
      ?.selectValue,
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入货号'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  imgList.value = tableDataList.list.map((item: any) => {
    return item.photoPath
  })
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  summaryData.value[0].num = tableDataList.inTotal
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 下拉框请求数据
const getSelectList = async () => {
  await selectList(formList, () => service.get('/pcOrder/getPcOrderDataRsjCondition'))
}
onMounted(() => {
  AcquisitionModel(15, formList)
  getSelectList()
})
const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
</script>

<style lang="less" scoped>
.susses {
  background-color: rgb(71, 232, 71);
  text-align: center;
  line-height: 25px;
  font-size: 20px;
}
.error {
  background-color: #ff0000;
  font-size: 20px;
  text-align: center;
}
</style>
